define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'goods/index' + location.search,
                    add_url: 'goods/add',
                    edit_url: 'goods/edit',
                    del_url: 'goods/del',
                    multi_url: 'goods/multi',
                    import_url: 'goods/import',
                    table: 'goods',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'sort',
                sortOrder:'id,desc',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'sort', operate:false, title: __('Sort')},
                        {field: 'shopapply.company_name', title: __('所属商家'),operate:'like'},
                        {field: 'name', title: __('Name'),operate:'like',formatter : function(value, row, index, field){

                                return ""+value+"";
                            },
                            cellStyle : function(value, row, index, field){
                                return {
                                    css: {"min-width": "150px",
                                        "white-space": "nowrap",
                                        "text-overflow": "ellipsis",
                                        "overflow": "hidden",
                                        "max-width":"300px"
                                    }
                                };
                            }},
                        {field: 'thumbnailimage', operate:false, title: __('Thumbnailimage'), events: Table.api.events.image, formatter: Table.api.formatter.image},
                        // {field: 'goods_attr', title: __('Goods_attr'), searchList: {"0":__('Goods_attr 0'),"1":__('Goods_attr 1'),"2":__('Goods_attr 2')}, formatter: Table.api.formatter.normal},
                        {field: 'category1.name', title: __('一级分类')},
                        {field: 'category2.name', title: __('二级分类')},
                        {field: 'category3.name', title: __('三级分类')},
                        {field: 'sell_num', title: __('Sell_num')},
                        {field: 'state', title: __('State'), searchList: {"0":__('State 0'),"1":__('State 1'),"2":__('State 2')}, formatter: Table.api.formatter.normal},
                        //0=正常商品,1=预约商品,2=预售商品,3=爆品
                        {field: 'type', title: __('类型'), searchList: {"0":__('正常商品'),"1":__('预约商品'),"2":__('预售商品'),"3":__('爆品')}, formatter: Table.api.formatter.normal},

                        {field: 'status', title: __('审核状态'), searchList: {"0":__('审核中'),"1":__('通过'),"2":__('驳回')}, formatter: Table.api.formatter.normal},
                        {field: 'max_price', operate:false, title: __('Max_price')},
                        {field: 'min_price', operate:false, title: __('Min_price'),},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate,
                            buttons: [
                                // {
                                //     name: '评价',
                                //     text: __('评价'),
                                //     icon: 'fa fa-list',
                                //     classname: 'btn btn-info btn-xs btn-detail btn-dialog',
                                //     url: "goods_comment/index/gid/{id}",
                                //
                                // },
                                {
                                    name: 'detail',
                                    text: __('评价'),
                                    title: __('评价'),
                                    classname: 'btn btn-xs btn-info btn-dialog',
                                    url: 'goods_comment/index?gid={ids}',
                                    extend: 'data-toggle="tooltip" data-area=\'["100%", "100%"]\'',
                                    callback: function (data) {

                                        Layer.alert("接收到回传数据：" + JSON.stringify(data), {title: "回传数据"});
                                    },
                                    visible: function (row) {
                                        //返回true时按钮显示,返回false隐藏
                                        // console.log(data)
                                        if(row.admin_id == 1){
                                            return true;
                                        }else{
                                            return false;
                                        }

                                    },
                                    hidden:function (data) {
                                        console.log(data);
                                        // if (data.random==2){
                                        //     return true;
                                        // }
                                    }
                                }
                            ],
            //
            //
            //             buttons: [
            //     {
            //         name: 'detail',
            //         text: __('题目列表'),
            //         title: __('题目列表'),
            //         classname: 'btn btn-xs btn-info btn-dialog',
            //         url: 'test_paper/index?test_bank_id={ids}',
            //         extend: 'data-toggle="tooltip" data-area=\'["100%", "100%"]\'',
            //         callback: function (data) {
            //             Layer.alert("接收到回传数据：" + JSON.stringify(data), {title: "回传数据"});
            //         },
            //         visible: function (row) {
            //             //返回true时按钮显示,返回false隐藏
            //             return true;
            //         },
            //         hidden:function (data) {
            //             console.log(data);
            //             if (data.random==2){
            //                 return true;
            //             }
            //         }
            //     }
            // ]
                            formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        recyclebin: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    'dragsort_url': ''
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: 'goods/recyclebin' + location.search,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'name', title: __('Name'), align: 'left'},
                        {
                            field: 'deletetime',
                            title: __('Deletetime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'operate',
                            width: '130px',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'Restore',
                                    text: __('Restore'),
                                    classname: 'btn btn-xs btn-info btn-ajax btn-restoreit',
                                    icon: 'fa fa-rotate-left',
                                    url: 'goods/restore',
                                    refresh: true
                                },
                                {
                                    name: 'Destroy',
                                    text: __('Destroy'),
                                    classname: 'btn btn-xs btn-danger btn-ajax btn-destroyit',
                                    icon: 'fa fa-times',
                                    url: 'goods/destroy',
                                    refresh: true
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});